package com.wocao.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wocao.entity.ReqBody;
import com.wocao.entity.ResponseResult;
import com.wocao.entity.ZyCommunity;
import com.wocao.service.ZyCommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 小区  前端控制器
 *
 * @since 2023-11-29
 */
@CrossOrigin
@RestController
@RequestMapping("/zyCommunity")
public class ZyCommunityController {
    @Autowired
    private ZyCommunityService zyCommunityService;

    //查询所有小区
    @GetMapping("communitys")
    public ResponseResult<List<ZyCommunity>> all() {
        List<ZyCommunity> list = zyCommunityService.list();
        return ResponseResult.ok(list);
    }

    @PostMapping("/selectAllZyCommunity/{page}")
    public ResponseResult selectAllZyCommunity(@PathVariable int page, @RequestBody ReqBody reqBody) {
        Page<ZyCommunity> zyCommunityPage = zyCommunityService.selectAllZyCommunitys(new Page(page, reqBody.getSize()), reqBody);
        return ResponseResult.ok(zyCommunityPage);
    }

    @PostMapping("/communityAdd")
    public ResponseResult communityAdd(@RequestBody ZyCommunity zyCommunity) {
        zyCommunityService.
                areaAdd(zyCommunity);
        return ResponseResult.ok("添加成功");
    }

    @PutMapping("/communityEdit")
    public ResponseResult communityEdit(@RequestBody ZyCommunity zyCommunity) {
        zyCommunityService.communityEdit(zyCommunity);
        return ResponseResult.ok("修改成功");
    }

    @DeleteMapping("/communityDelete/{communityId}")
    public ResponseResult communityDelete(@PathVariable long communityId) {
        zyCommunityService.removeById(communityId);
        return ResponseResult.ok("删除成功！！！");
    }

    @DeleteMapping("/communityDeletes")
    public ResponseResult communityDeletes(@RequestParam String[] communityIds) {
        zyCommunityService.removeByIds(Arrays.asList(communityIds));
        return ResponseResult.ok("批量删除成功！！！");
    }

    //导出
    @GetMapping("/derive")
    public void derive(HttpServletResponse response, @RequestParam("communityIds") String[] communityIds) throws IOException {
        zyCommunityService.derive(response, communityIds);
    }

    @GetMapping("communities")
    public ResponseResult<ZyCommunity> findCommunity() {
        try {
            List<ZyCommunity> communities = zyCommunityService.list();
            return ResponseResult.ok(communities);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500l, e.getMessage());
        }
    }

    //更改物业
    @PutMapping("/editDeptId")
    public ResponseResult editDeptId(@RequestBody ReqBody reqBody) {
        zyCommunityService.editDeptId(reqBody.getCommunityId(),reqBody.getDeptId());
        return ResponseResult.ok("物业更改成功");
    }


}
